#include <iostream>
using namespace std;
#include<string>
//继承：子类可以将基类中成员都可以继承下来
class Base
{
public:
	void SetBaseInfo(int pub, int pro, int pri)
	{
		_pub = pub;
		_pro = pro;
		_pri = pri;
	}

	void Print()
	{
		cout << _pub << " " << _pro << " " << _pri << endl;
	}

public:
	int _pub;
protected:
	int _pro;
private:
	int _pri;
};

// 子类 派生类
class Derived : public Base
{};

int main()
{
	cout << sizeof(Derived) << endl;

	Derived d;
	
	d.SetBaseInfo(1, 2, 3);
	d.Print();
	return 0;
}



// 不同继承方式下，基类不同访问权限的成员在子类中权限
// 父类 基类
class Base
{
public:
	void SetBaseInfo(int pub, int pro, int pri)
	{
		_pub = pub;
		_pro = pro;
		_pri = pri;
	}

	void Print()
	{
		cout << _pub << " " << _pro << " " << _pri << endl;
	}

public:
	int _pub;
protected:
	int _pro;
private:
	int _pri;
};

// 子类 派生类
// public继承方式
class Derived :public Base
{
public:
	void SetDerived()
	{
		_pub = 10;      // 基类中public修饰的成员在子类中的访问权限也是public
		_pro = 20;      // 基类中protected修饰的成员在子类中的访问权限也是protected
		//_pri = 30;   // 基类中私有的成员变量不能在子类中被访问---在子类中不可见
	}

private:
	int _d;
};

class D : public Derived
{
public:
	void fun()
	{
		_pub = 10;
		_pro = 2000;
	}
};
int main()
{
	cout << sizeof(Derived) << endl;

	Derived d;
	d.SetBaseInfo(1, 2, 3);
	d.Print();

	// d._pub = 100;
	// d._pro = 200;
	return 0;
}
class Base
{
public:
	void SetBaseInfo(int pub, int pro, int pri)
	{
		_pub = pub;
		_pro = pro;
		_pri = pri;
	}

	void Print()
	{
		cout << _pub << " " << _pro << " " << _pri << endl;
	}

public:
	int _pub;
protected:
	int _pro;
private:
	int _pri;
};

// 子类 派生类
// struct定义的类：默认情况下继承权限是public
// class定义的类：默认情况下继承权限是private
struct Derived : Base
{
public:
	void SetDerived()
	{
		_pub = 10;      // 基类中public修饰的成员在子类中的访问权限也是private
		_pro = 20;      // 基类中protected修饰的成员在子类中的访问权限也是private
		// _pri = 30;   // 基类中私有的成员变量不能在子类中被访问---在子类中不可见
	}

private:
	int _d;
};

class D : public Derived
{
public:
	void func()
	{
		_pub = 10;
		_pro = 20;
	}
};
int main()
{
	Derived d;
	d._pub = 10;
	//d._pro = 13;
	return 0;
}
class Animal
{
public:
	void SetAnimal(const string& name, const string& gender, int age)
	{
		_name = name;
		_gender = gender;
		_age = age;
	}

	void Sleep()
	{
		cout << _name << "在睡觉" << endl;
	}
protected:
	string _name;
	string _gender;
	int _age;
};

class Dog : public Animal
{
public:
	void Bark()
	{
		cout << _name << "旺旺旺" << endl;
	}
	//protected:
	string _color;
};


int main()
{
	Animal animal;
	Dog dog;

	animal = dog;
	//dog = animal;

	Animal* pa = &dog;
	
	Dog* pg = (Dog*)&animal;
	
	// pg->_color = "大黄";  // 报错
	
	
	Animal& ra = dog;
	// Dog& rd = animal;
	return 0;
}

同名隐藏
class Base
{
public:
	void SetInfo(int b)
	{
		_b = b;
	}

	void func()
	{
		cout << "Base::func()" << endl;
	}
	int _b;
};

class Derived : public Base
{
public:
	// 子类将来会从基类中继承 void SetInfo(int b)
	void SetInfo(int b, int d)
	{
		_b = b;
		_d = d;
	}

	void func()
	{
		_b = 100;
		//Base::_b = 200;
		//__super::_b = 20;
		cout << "Derived::func()" << endl;
	}

	int _d;
	//int _b;
	char _b;
};

int main()
{
	Derived d;
	

	d.SetInfo(1, 2);
	d.Base::SetInfo(10);

	//d._b = 100;
	//d.Base::_b = 200;

	d.func();
	d.Base::func();
	return 0;
}
// 子类的构造方法如何实现？
class Base
{
public:
	/*Base(int b = 0)
	: _b(b)
{}*/

	Base()
		: _b(0)
	{}

	void SetBase(int b)
	{
		_b = b;
	}

protected:
	int _b;
};

// 如果基类的构造方法没有显式提供，则子类的构造方法可以提供也可以不提供
class Derived : public Base
{
public:
	Derived(int b, int d)
		
	{
		_b = b;
		_d = d;
	}
protected:
	int _d;
};


int main()
{
	Derived d(1, 2);
	return 0;
}

class Base
{
public:
	Base(int b)
		: _b(b)
	{}

	void SetBase(int b)
	{
		_b = b;
	}

protected:
	int _b;
};

// 如果基类的构造方法是带有参数的，但是不是全缺省的构造方法，
// 则子类的构造方法必须要提供，并且必须要在子类构造方法初始化
// 位置显式调用基类的构造方法将子类对象中从基类继承下来的部分
// 初始化完成
class Derived : public Base
{
public:
	Derived(int b, int d)
		: Base(b)
		, _d(d)
	{}

protected:
	int _d;
};

int main()
{
	Derived d(1, 2);
	return 0;
}

class Base
{
public:
	Base(int b = 0)
	: _b(b)
	{}

	//Base()
	//	: _b(0)
	//{}

	void SetBase(int b)
	{
		_b = b;
	}

protected:
	int _b;
};

// 如果基类的构造方法是无参或者全缺省的，
// 则子类的构造方法可以提供也可以不提供
class Derived : public Base
{
public:
	Derived(int b, int d)
		: Base(2)
		, _d(0xcccccccc)
	{
		_b = b;
		_d = d;
	}
protected:
	int _d;
};


int main()
{
	Derived d(1, 2);
	return 0;
}

